package cn.jxau.common.util;
/**
 * 该类代表一个邮件服务器，用来发送Emali
 * @author licho
 *
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

import cn.jxau.bean.vo.Email;

public class MailServer {
	private static Logger log=Logger.getLogger(MailServer.class);
	private static Session session;//Mail的一个对话，该对象可以创建为单例。
	private static Properties emailPro=new Properties();//邮件服务器相关配置的properties文件
	private static String sender;//发送者
	private static String pwd;//发送者邮箱密码
	private static EmailAuthenticator authen=new EmailAuthenticator();
	static
	{
		sender=ProUtils.getProperty("email.sender");
		pwd=ProUtils.getProperty("email.pwd");
		InputStream in=MailServer.class.getClassLoader().getResourceAsStream("emailServer.properties");
		try {
			emailPro.load(in);
			session=Session.getInstance(emailPro,authen);
			if(log.isDebugEnabled())
				session.setDebug(true);
		} catch (IOException e) {
			if(log.isDebugEnabled())
				log.debug("Email 服务器参数加载失败!");
		}
	}
	
	
	/**
	 * 创建一封只包含文本的简单邮件
	*@param email 需要发送的邮件
	 * @return
	 */
	private static MimeMessage createMimeMessage(Email email){
		MimeMessage message=new MimeMessage(session);//创建Message
		try {
			//1.设置发件人
			message.setFrom(new InternetAddress(sender, "美文驿站", "UTF-8"));
			//2.设置发件人
			message.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(email.getReciver(),email.getNickName(),"UTF-8"));
			//3.设置主题
			message.setSubject(email.getSubject(),"UTF-8");
			//4.设置内容
			message.setContent(email.getContent(), "text/html;charset=UTF-8");
			//5.设置时间
			message.setSentDate(new Date());
			//6.保存设置
			message.saveChanges();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return message;
	}
	
	/**
	 * 发送Email
	 * @param email
	 * @return
	 * @throws MessagingException 
	 */
	public static boolean sendEmail(Email email){
		//1.创建一封邮件
		MimeMessage message=createMimeMessage(email);
		//2.根据session获取传输对象
		try {
			Transport.send(message);
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	
	protected static class EmailAuthenticator extends Authenticator{
		private String sender=ProUtils.getProperty("email.user");
		private String password=ProUtils.getProperty("email.pwd");
		@Override
		protected PasswordAuthentication getPasswordAuthentication() {
			// TODO Auto-generated method stub
			return new PasswordAuthentication(sender, password);
		}
	}
}
